<template>
	<sw-common-frame :loading="loading">
		<template slot="button">
			<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<el-form slot="query" size="small" inline>
			<el-form-item>
				<el-input v-model="queryData.name" clearable placeholder="角色名称" style="width: 180px"></el-input>
			</el-form-item>
			<el-form-item>
				<el-select v-model="queryData.userType" clearable placeholder="角色类型" style="width: 114px">
					<el-option v-for="(item, index) in roleTypes" :key="index" :label="item.deName" :value="item.deValue"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="queryTableData()">查询</el-button>
			</el-form-item>
		</el-form>
		<el-table slot="table" size="medium" :data="tableData" stripe height="100%">
			<el-table-column label="角色名称" prop="name" min-width="200" show-overflow-tooltip></el-table-column>
			<el-table-column label="数据权限等级" min-width="160" :formatter="dataScopeFormatter"></el-table-column>
			<el-table-column label="角色描述" prop="content" min-width="200" show-overflow-tooltip></el-table-column>
			<el-table-column label="角色类型" min-width="120" :formatter="roleTypeFormatter"></el-table-column>
			<el-table-column label="创建时间" prop="createTime" min-width="160"></el-table-column>
			<el-table-column label="操作" width="210">
				<el-button-group slot-scope="{ row }">
					<el-button size="mini" type="info" @click="handleConfigMenu(row)">配置菜单</el-button>
					<el-button size="mini" type="warning" @click="handleOpenDialog(row.id)">修改</el-button>
					<el-button size="mini" type="danger" @click="handleDeleteData(row)">删除</el-button>
				</el-button-group>
			</el-table-column>
		</el-table>
		<template slot="page">
			<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData()"></sw-pagination>
		</template>
		<form-dialog v-model="formDialog" :data-id="dataId" :list="roleTypes" @refresh="queryTableData()"></form-dialog>
		<config-menu v-model="menuDrawer" :data-id="dataId"></config-menu>
	</sw-common-frame>
</template>

<script>
import formDialog from './form';
import configMenu from './configMenu';
import listMixins from '@/mixins/listMixins';
import { dataPermissions } from './dataScope';
import { getRoleList, deleteRole } from '@/api/system/tbSysRole';
export default {
	mixins: [listMixins],
	enums: ['TB_SYS_USER_TYPE'],
	components: { formDialog, configMenu },
	data() {
		return {
			pageName: '角色',
			menuDrawer: false,
			userDialog: false,
			selectUsers: [],
			submitLoading: false
		};
	},
	computed: {
		listFunction() {
			return eval(getRoleList);
		},
		deleteFunction() {
			return eval(deleteRole);
		},
		roleTypes() {
			return this.dictData['TB_SYS_USER_TYPE'];
		}
	},
	methods: {
		//配置菜单
		handleConfigMenu(row) {
			this.dataId = row.id;
			this.menuDrawer = true;
		},

		//数据权限
		dataScopeFormatter(data) {
			let info = dataPermissions.find((n) => n.value === data.dataScope);
			return info?.label || '未知';
		},

		//角色类型
		roleTypeFormatter(data) {
			let info = this.roleTypes.find((n) => n.deValue === data.userType);
			return this.$createElement('elTag', { attrs: { size: 'medium' } }, info?.deName || '未知');
		},

		formatConfigParams(arr) {
			let params = { roleId: this.dataId };
			params['userList'] = arr.map((item) => {
				let obj = { relId: item.relId };
				if (item.oper === 'add') {
					obj['operType'] = 1;
				} else if (item.oper === 'del') {
					obj['operType'] = 2;
				}
				return { ...obj };
			});
			this.submitLoading = true;
			return params;
		},

		async handleSubmitSelect(arr, list) {
			let params = this.formatConfigParams(arr);
			let result = await addUsersByRole(params).then((res) => res.data);
			this.submitLoading = false;
			if (result.code === '0') {
				this.$message.success('配置角色人员成功');
				this.userDialog = false;
			}
		}
	}
};
</script>

<style scoped></style>
